package com.wangwang.dengluServlet;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



import com.wangwang.entity.yonghu;

import com.wangwang.service.denglu;

/**
 * Servlet Filter implementation class lanjie
 */
@WebFilter(filterName="userFilter",urlPatterns="/src/views/index.html")
public class lanjie implements Filter {

    /**
     * Default constructor. 
     */
    public lanjie() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request1=(HttpServletRequest) request;
		HttpServletResponse response1=(HttpServletResponse) response;
		
		HttpSession session = request1.getSession();
//        session.setAttribute("name", request.getParameter ("name"));
//        session.setAttribute("pwd", request.getParameter ("pwd"));
		//HttpSession session=request2.getSession();
		String name=(String)session.getAttribute("name");
		String pwd=(String)session.getAttribute("pwd");
		
		denglu m=new denglu();
	 

		yonghu u=m.chaxun(name);
		
		if(u.getName()!=null&&u.getName().equals(name)&&u.getPwd().equals(pwd)) {
			
			chain.doFilter(request, response);
			return;
		}else {
		response1.sendRedirect("/wangwang/src/views/user/login.html");
		return;
		}
		}
	

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
